import javax.swing.*;
import java.awt.*;
import java.io.*;

public class EditorPanel {
    JFileChooser fileChooser = new JFileChooser();

    public EditorPanel() {
        JMenu file = new JMenu("文件");
        JMenu edit = new JMenu("编辑");
        JMenuBar bar = new JMenuBar();
        JFrame frame = new JFrame("记事本");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setSize(500, 300);
        frame.setLocationRelativeTo(null);
        JTextArea area = new JTextArea();
        area.setForeground(Color.BLUE);
        JScrollPane scrollPane = new JScrollPane(area);
        createFileMenu(frame, file, area);
        createEditMenu(edit, area);
        addComponent(bar, file, edit);
        addComponent(frame, scrollPane);
        frame.setJMenuBar(bar);
        frame.setVisible(true);
    }

    void createFileMenu(JFrame frame, JMenu file, JTextArea area) {
        JMenuItem open = new JMenuItem("打开");
        JMenuItem close = new JMenuItem("关闭");
        JMenuItem save = new JMenuItem("保存");
        close.addActionListener(e -> {
            frame.dispose();
        });
        open.addActionListener(e -> {
            int t = fileChooser.showDialog(frame, "打开文件");
            if (t == JFileChooser.APPROVE_OPTION) {
                File f = fileChooser.getSelectedFile();
                try {
                    FileReader reader = new FileReader(f);
                    BufferedReader bufferedReader = new BufferedReader(reader);
                    String str = "";
                    String line = bufferedReader.readLine();
                    while (line != null) {
                        str += line + "\n";
                        line = bufferedReader.readLine();
                    }
                    area.setText(str);
                } catch (IOException fileNotFoundException) {
                    fileNotFoundException.printStackTrace();
                }
            }
        });
        save.addActionListener(e -> {
            if (fileChooser.showDialog(frame, "文件保存") == JFileChooser.APPROVE_OPTION) {
                File f = fileChooser.getSelectedFile();
                try {
                    FileOutputStream out = new FileOutputStream(f);
                    out.write(area.getText().getBytes());
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
            }
        });
        addComponent(file, open, save, close);
    }

    void createEditMenu(JMenu edit, JTextArea area) {
        JMenuItem copy = new JMenuItem("复制");
        JMenuItem paste = new JMenuItem("粘贴");
        JMenuItem cut = new JMenuItem("剪切");
        JMenuItem delete = new JMenuItem("删除");
        JMenuItem all = new JMenuItem("全选");
        copy.addActionListener(e -> {
            area.copy();
        });
        paste.addActionListener(e -> {
            area.paste();
        });
        cut.addActionListener(e -> {
            area.cut();
        });
        delete.addActionListener(e -> {
            area.replaceSelection(null);
        });
        all.addActionListener(e -> {
            area.selectAll();
        });
        addComponent(edit, copy, paste, cut, delete, all);
    }

    void addComponent(JFrame frame, Component... objs) {
        for (Component obj : objs) {
            frame.add(obj);
        }
    }

    void addComponent(JMenuBar bar, Component... objs) {
        for (Component obj : objs) {
            bar.add(obj);
        }
    }

    void addComponent(JMenu menu, Component... objs) {
        for (Component obj : objs) {
            menu.add(obj);
        }
    }

    public static void main(String[] args) {
        new EditorPanel();
    }
}
